package com.lxb.myforum.service

import java.util.Date

import com.lxb.myforum.base.BaseService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

import com.lxb.myforum.pojo.Comment
import com.lxb.myforum.repository.CommentRepository
import com.lxb.myforum.service.spec.CommentSpec

@Service
class CommentService @Autowired
constructor(private val commentRepository: CommentRepository) : BaseService() {

    fun findComments(comment: Comment, pageable: Pageable): Page<Comment> {
        return commentRepository.findAll(CommentSpec(comment), pageable)
    }

    fun count(): Long {
        return commentRepository.count()
    }

    fun count(comment: Comment): Long {
        return commentRepository.count(CommentSpec(comment))
    }

    fun saveComment(comment: Comment): Comment {
        if (comment.id == null) {
            comment.createTime = Date()
        }
        comment.updateTime = Date() // 更新时间一直在变的
        return commentRepository.save(comment)
    }
}
